package com.kaishengit.web;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kaishengit.pojo.Goods;
import com.kaishengit.pojo.GoodsBack;
import com.kaishengit.pojo.Goodsin;
import com.kaishengit.pojo.Inventory;
import com.kaishengit.pojo.Operator;
import com.kaishengit.pojo.SalesRecords;
import com.kaishengit.service.GoodsService;
import com.kaishengit.service.InventoryService;
import com.kaishengit.service.OperatorService;
import com.kaishengit.service.SalesRecordsService;

@Controller
public class AppController {

	private OperatorService operatorService;
	private GoodsService goodsService;
	private Operator operator;
	private InventoryService inventoryService;
	private SalesRecordsService salesRecordsService;
	@RequestMapping("/index")
	public String toLogin(){
		return "login";
	}
	@RequestMapping("/login")
	public String login(Operator ope,HttpSession session){
		
		operator = operatorService.findByNameByPassword(ope);
		session.setAttribute("operator", operator);
		if(operator != null){
			return "main";
		}else{
			return "login";
		}
		
	}
	@RequestMapping("/main")
	public String main(){
		return "main";
	}
	@RequestMapping("/goodsin")
	public String goodsIn(Model model){
		List<Goods> goodsList = goodsService.findAll();
		model.addAttribute("goodsList", goodsList);
		return "goodsin";
	}
	@RequestMapping("/getGoodsin")
	public @ResponseBody Goodsin getGoodsin(){
		Goodsin goodsIn = new Goodsin();
		goodsIn.setId("PI"+ new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()).toString());
		goodsIn.setTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
		goodsIn.setOperator(operator);
		return goodsIn;
	}
	@RequestMapping("/goodsBack")
	public String goodsBack(Model model){
		List<Inventory> inventoryList = inventoryService.findAll();
		model.addAttribute("inventoryList", inventoryList);
		return "goodsBack";
	}
	@RequestMapping("/getGoodsBack")
	public @ResponseBody GoodsBack getGoodsBack(){
		GoodsBack goodsBack = new GoodsBack();
		goodsBack.setId("PO"+ new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()).toString());
		goodsBack.setTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
		goodsBack.setOperator(operator);
		return goodsBack;
	}
	@RequestMapping(value="/getGoods{id}",method=RequestMethod.GET)
	public @ResponseBody Goods getGoods(@PathVariable String id){
		Goods goods = goodsService.findById(id);
		return goods;
	}
	@RequestMapping("/sales")
	public String sales(Model model){
		List<Inventory> inventoryList = inventoryService.findAll();
		model.addAttribute("inventoryList", inventoryList);
		return "sales";
	}
	@RequestMapping(value="/getSalesGoods{id}",method=RequestMethod.GET)
	public @ResponseBody Goods getSalesGoods(@PathVariable String id){
		Goods goods = goodsService.findById(id);
		goods.setPrice(goods.getPrice() + (goods.getPrice() * 0.3));
		return goods;
	}
	@RequestMapping(value="/getSalesRecords{id}",method=RequestMethod.GET)
	public @ResponseBody SalesRecords getSalesRecords(@PathVariable String id){
		SalesRecords salesRecords = salesRecordsService.findById(id);
		return salesRecords;
	}
	@RequestMapping("/getSales")
	public @ResponseBody Goodsin getSales(){
		Goodsin goodsIn = new Goodsin();
		goodsIn.setId("PS"+ new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()).toString());
		goodsIn.setTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
		goodsIn.setOperator(operator);
		return goodsIn;
	}
	@RequestMapping("/getSalesBack")
	public @ResponseBody GoodsBack getSalesBack(){
		GoodsBack goodsBack = new GoodsBack();
		goodsBack.setId("PB"+ new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()).toString());
		goodsBack.setTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
		goodsBack.setOperator(operator);
		return goodsBack;
	}
	@RequestMapping("/look")
	public String look(){
		return "look";
	}
	@RequestMapping("/stock")
	public String stock(Model model){
		List<Inventory> inventoryList = inventoryService.findAll();
		model.addAttribute("inventoryList", inventoryList);
		return "inventoryList";
	}
	@RequestMapping("/system")
	public String system(Model model){
		return "system";
	}
	
	//set
	@Autowired
	public void setOperatorService(OperatorService operatorService) {
		this.operatorService = operatorService;
	}
	@Autowired
	public void setGoodsService(GoodsService goodsService) {
		this.goodsService = goodsService;
	}
	@Autowired
	public void setInventoryService(InventoryService inventoryService) {
		this.inventoryService = inventoryService;
	}
	@Autowired
	public void setSalesRecordsService(SalesRecordsService salesRecordsService) {
		this.salesRecordsService = salesRecordsService;
	}
	
	
}
